// Function handle all cross links
// contain ajax helper class to
// using for ajax action link request
var leadSourceId;       // Current Lead source Id
var leadSourceTitle;

function isNumberKey(evt) {
	evt = evt || window.event;
	var charCode = (evt.which) ? evt.which : evt.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

function ShowMessage(message) {
	$("#message").html(message);
	ShowDialog(true);
}

function doDeleteLinks() {
	if (document.getElementsByTagName) {
		var links = document.getElementsByTagName("a");
		for (var i = 0; i < links.length; i++) {
			if (links[i].className.match("delete")) {
				links[i].onclick = function () {
					leadSourceId = this.getAttribute("href").replace("##", "");
					GetLeadSourceTitleById(leadSourceId);
					// Show confirm delete modal popup
					ShowConfirmDelete();

				};
			}
		}
	}
}

function GetLeadSourceTitleById(leadSourceId) {
	for (var i = 0; i < initialData.length; i++) {
		if (initialData[i].LeadSourceId == leadSourceId) {
			leadSourceTitle = initialData[i].Title;
			//alert("leadSourceTitle:" + leadSourceTitle);
			break;
		}
	}
}

function ShowConfirmDelete() {
	$("#title").text("Confirm delete Lead Source");
	$("#LeadSourceTitle").text(leadSourceTitle);

	ShowDialog(true);
}

function Delete(leadSourceId) {
	/*try {
		//alert("leadSourceId need delete:" + leadSourceId);
		alert("leadSourceId length:" + initialData[1].length() + "," + initialData[2].length());
		if (initialData !== undefined) {
			for (var i = 0; i < initialData.length; i++) {
				//alert("Delete LeadSourceId:" + initialData[i].LeadSourceId);
				if (initialData[i].LeadSourceId == leadSourceId) {
					alert("Delete LeadSourceId:" + LeadSourceId);

					//initialData.splice(i, 1);
					//initialData[i].Status = enable;
					//Remove item here

					break;
				}
			}
		}
	}
	catch (e) {
	}*/
	$.getJSON('/LeadSource/Delete/' + leadSourceId, function (data) {
		if (data == "") {       // Delete successful.
			HideDialog();
			//alert("Delete successful");
			// Only use when integreated with knockoutjs
			try {
				if (initialData !== undefined) {
					for (var i = 0; i < initialData.length; i++) {
						//alert("Delete LeadSourceId:" + initialData[i].LeadSourceId);
						if (initialData[i].LeadSourceId == leadSourceId) {
							//Remove item here
							initialData.splice(i, 1);
							RefreshKo();
							//alert("Delete LeadSourceId:" + LeadSourceId);
							break;
						}
					}
				}
			}
			catch (e) {
			}
		}
		else {
			HideDialog();
			$("#message").text(data);
			ShowDialogError(true);
		}

	});
}

function RefreshKo() {
	ko.applyBindings(new PagedGridModel(initialData));
}

function ShowDialog(modal) {
	$("#overlay").show();
	$("#dialog").fadeIn(300);

	if (modal) {
		$("#overlay").unbind("click");
	}
	else {
		$("#overlay").click(function (e) {
			HideDialog();
		});
	}
}

function ShowDialogError(modal) {
	$("#overlay").show();
	$("#dialogError").fadeIn(300);

	if (modal) {
		$("#overlay").unbind("click");
	}
	else {
		$("#overlay").click(function (e) {
			HideDialogError();
		});
	}

	//$("#title").text("Delete failed!!!");

	// Click OK button to close error message
	$("#btnOK").click(function (e) {
		HideDialogError();
	});
}

function HideDialog() {
	$("#overlay").hide();
	$("#dialog").fadeOut(300);
}

function HideDialogError() {
	$("#overlay").hide();
	$("#dialogError").fadeOut(300);
}

/*
	Load Email Template Using JSON
	Calculate pagesize, pagecurrent, list items
	When user change pagesize and pagecurrent

*/

$(document).ready(function () {
	// Previous button
	$('#lnkPrevious').click(function () {
		LoadEmailTemplate($(this).attr('rel'));
	});

	// Next button
	$('#lnkNext').click(function () {
		LoadEmailTemplate($(this).attr('rel'));
	});

	// Next button
	$('#lnkRefresh').click(function () {
		LoadEmailTemplate($(this).attr('rel'));
	});

	// Close dialog
	$("#btnClose").click(function (e) {
		HideDialog();
		e.preventDefault();
	});
	// Close model
	$(".close").click(function (e) {
		HideDialogError();
		e.preventDefault();
	});
	// Cancel delete
	$("#btnCancel").click(function (e) {
		HideDialog();
		e.preventDefault();
	});
	// Implement delete
	$("#btnSubmit").click(function (e) {
		Delete(leadSourceId);
	});
	$.ajaxSetup({
		error: function (x, e, errorThrown) {
			$("#message").text(x.responseText);
			HideDialog();
			ShowDialogError(true);
		}
	});
});


// Window load
window.addLoadEvent(function() { setInterval("doDeleteLinks()", 100); });
